package ldkxingzhe.l365calendar.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.widget.dialog.DialogFragmentBase;
import ldkxingzhe.l365calendar.widget.dialog.picker.TimePicker;

/**
 * 全天日程提醒时间设置
 * Created by xingzhe on 16/2/1.
 */
public class ScheduleAlertSetupFragment extends AllEditFragmentBase
        implements View.OnClickListener, HandleFragmentResult {
    @SuppressWarnings("unused")
    private static final String TAG = "ScheduleAlertSetupFragment";

    public static final String TIME_LONG = "time_long";

    private long timeLong;
    private TextView timeTextView;
    private View timeLayout;

    public static ScheduleAlertSetupFragment newInstance(Bundle bundle) {
        long time = bundle.getLong(TIME_LONG);
        if(time < 0) time = 0;
        Bundle args = new Bundle();
        args.putLong(TIME_LONG,time);
        ScheduleAlertSetupFragment fragment = new ScheduleAlertSetupFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        timeLong = getArguments().getLong(TIME_LONG);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.fragment_schedule_alert_set_up, container, false);
        timeTextView = (TextView) resultView.findViewById(R.id.text);
        timeLayout = resultView.findViewById(R.id.alert_layout);
        timeLayout.setOnClickListener(this);
        setTimeText((int)timeLong/60/1000/60, (int)timeLong/60/1000%60);
        return resultView;
    }

    private void setTimeText(int hour, int minute){
        timeTextView.setText(formatHM(hour, minute));
    }

    @NonNull
    public static String formatHM(int hour, int minute) {
        StringBuilder builder = new StringBuilder();
        if(hour < 10){
            builder.append(0);
        }
        builder.append(hour);
        builder.append(":");
        if(minute < 10){
            builder.append(0);
        }
        builder.append(minute);
        return builder.toString();
    }

    @Override
    public void onClick(View v) {
        FragmentManager manager = getFragmentManager();
        DialogFragmentBase base = (DialogFragmentBase) manager.findFragmentByTag("Dialog");
        if(base == null){
            base = TimePicker.newInstance((int)timeLong/60/1000/60, (int)timeLong/60/1000%60);
        }else{
            base.dismiss();
        }
        base.setTargetFragment(this, 0);
        base.show(manager, "Dialog");
    }

    @Override
    public void onSaveClick() {
        Intent intent = new Intent();
        intent.putExtra(ScheduleAlertSetupFragment.TIME_LONG, timeLong);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
    }

    @Override
    public void onHandleFragmentResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == Activity.RESULT_OK){
            setIsChanged(true);
            int hour = data.getIntExtra(TimePicker.HOUR, 0);
            int minute = data.getIntExtra(TimePicker.MINUTE, 0);
            timeLong = (hour * 60 + minute) * 60 * 1000;
            setTimeText(hour, minute);
        }
    }
}
